<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="爬虫爬取的时间戳" prop="rDate">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.rDate"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择爬虫爬取的时间戳">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用1" prop="othI1">-->
<!--        <el-input-->
<!--          v-model="queryParams.othI1"-->
<!--          placeholder="请输入备用1"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用4" prop="rDate1">-->
<!--        <el-date-picker clearable-->
<!--          v-model="queryParams.rDate1"-->
<!--          type="date"-->
<!--          value-format="yyyy-MM-dd"-->
<!--          placeholder="请选择备用4">-->
<!--        </el-date-picker>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="备用5" prop="othC3">-->
<!--        <el-input-->
<!--          v-model="queryParams.othC3"-->
<!--          placeholder="请输入备用5"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['reptilebytime:reptile:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['reptilebytime:reptile:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['reptilebytime:reptile:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['reptilebytime:reptile:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="reptileList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="每次爬取内容的id" align="center" prop="rId" />-->
      <el-table-column label="爬虫爬取的内容" align="center" prop="rText" >
        <template slot-scope="scope">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item
              :title="(function() { return scope.row.rText ? scope.row.rText.slice(0, 15) : '默认标题'; })()"
              :name="`unique-name-${scope.row.rText}`">
              <div>{{ scope.row.rText }}</div>
            </el-collapse-item>
          </el-collapse>
        </template>
      </el-table-column>

      <el-table-column label="词频结果" align="center" prop="rResult" >
        <template slot-scope="scope">
          <el-collapse v-model="activeNames" @change="handleChange">
            <el-collapse-item
              :title="(function() { return scope.row.rResult ? scope.row.rResult.slice(0, 15) : '默认标题'; })()"
              :name="`unique-name-${scope.row.rResult}`">
              <div>{{ scope.row.rResult }}</div>
            </el-collapse-item>
          </el-collapse>
        </template>
      </el-table-column>

<!--      <el-table-column label="爬虫爬取的时间戳" align="center" prop="rDate" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.rDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="类型" align="center" prop="type" />-->
<!--      <el-table-column label="备用1" align="center" prop="othI1" />-->
<!--      <el-table-column label="备用2" align="center" prop="othC1" />-->
<!--      <el-table-column label="备用3" align="center" prop="othC2" />-->
      <el-table-column label="词云图" align="center" prop="type" width="180">
        <template slot-scope="scope">
          <el-popover placement="left-start" trigger="click">
            <div class="row_reserve" style="height: 600px; width: 600px; border-radius: 60px">
              <img :src = "require(`@/assets/spider/${scope.row.type}`)">
            </div>
            <div slot="reference">
              <img :src = "require(`@/assets/spider/${scope.row.type}`)" width="100" height="100">
            </div>
          </el-popover>
        </template>
      </el-table-column>

      <el-table-column label="玫瑰图" align="center" prop="rResult" style="width: -moz-fit-content">
        <template slot-scope="scope">
          <el-badge align="center">
            <e-charts class="chart2" :option="option2(scope.row.tpResult)"/>
          </el-badge>
          <!--          <h1>{{ data }}</h1>-->
        </template>
      </el-table-column>

      <el-table-column label="备用4" align="center" prop="rDate1" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.rDate1, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="备用5" align="center" prop="othC3" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['reptilebytime:reptile:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['reptilebytime:reptile:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改定时爬虫对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="爬虫爬取的内容" prop="rText">
          <el-input v-model="form.rText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="词频结果" prop="rResult">
          <el-input v-model="form.rResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="爬虫爬取的时间戳" prop="rDate">
          <el-date-picker clearable
            v-model="form.rDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择爬虫爬取的时间戳">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用1" prop="othI1">
          <el-input v-model="form.othI1" placeholder="请输入备用1" />
        </el-form-item>
        <el-form-item label="备用2" prop="othC1">
          <el-input v-model="form.othC1" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备用3" prop="othC2">
          <el-input v-model="form.othC2" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="备用4" prop="rDate1">
          <el-date-picker clearable
            v-model="form.rDate1"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择备用4">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备用5" prop="othC3">
          <el-input v-model="form.othC3" placeholder="请输入备用5" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listReptile, getReptile, delReptile, addReptile, updateReptile } from "@/api/reptilebytime/reptile";

export default {
  name: "Reptile",
  data() {
    return {
      data: [],
      rawData: [],

      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 定时爬虫表格数据
      reptileList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        rText: null,
        rResult: null,
        rDate: null,
        type: null,
        othI1: null,
        othC1: null,
        othC2: null,
        rDate1: null,
        othC3: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    option2(rawData) {
      //this.processedData(rawData)
      return {
        calculable: true,
        series: [
          {
            name: '面积模式',
            type: 'pie',
            center: ['25%', 150],
            radius: [15, 70],
            roseType: 'area',
            max: 30,
            sort: 'ascending',
            data: [
              { value: 40, name: 'rose 1' },
              { value: 38, name: 'rose 2' },
              { value: 32, name: 'rose 3' },
              { value: 30, name: 'rose 4' },
              { value: 28, name: 'rose 5' },
              { value: 26, name: 'rose 6' },
            ]
            // data: [
            //   {value: this.data[0].value, name: this.data[0].name},
            //   {value: this.data[1].value, name: this.data[1].name},
            //   {value: this.data[2].value, name: this.data[2].name},
            //   {value: this.data[3].value, name: this.data[3].name},
            //   {value: this.data[4].value, name: this.data[4].name},
            //   {value: this.data[5].value, name: this.data[5].name},
            // ]
          }
        ]
      }
    },

    processedData(rawData) {
      // 将 rawData 转换为所需格式的逻辑,获取前十个数据
      let pairs = rawData.split(',').slice(0, 10);
      let data = []
      pairs.forEach(pair => {
        pair = pair.trim();
        if (pair) {
          let [word, value] = pair.split(':');
          data.push({
            name: word, // 使用 'name' 作为键名，这是 ECharts 等图表库常用的键名
            value: parseInt(value, 10) // 将值转换为整数，指定基数为 10
          });
        }
      });
      this.data = data
    },

    /** 查询定时爬虫列表 */
    getList() {
      this.loading = true;
      listReptile(this.queryParams).then(response => {
        this.reptileList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        rId: null,
        rText: null,
        rResult: null,
        rDate: null,
        type: null,
        othI1: null,
        othC1: null,
        othC2: null,
        rDate1: null,
        othC3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.rId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加定时爬虫";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const rId = row.rId || this.ids
      getReptile(rId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改定时爬虫";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.rId != null) {
            updateReptile(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addReptile(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const rIds = row.rId || this.ids;
      this.$modal.confirm('是否确认删除定时爬虫编号为"' + rIds + '"的数据项？').then(function() {
        return delReptile(rIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('reptilebytime/reptile/export', {
        ...this.queryParams
      }, `reptile_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.chart2{
  height: 300px;
  width: 350px;
}
</style>
